package com.lingdian.fund.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import jodd.datetime.JDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;

import com.lingdian.fund.bean.Fund;
import com.lingdian.fund.repository.FundDataReporsitory;
import com.lingdian.fund.repository.FundRepository;
import com.lingdian.fund.service.FundDataService;
import com.lingdian.fund.vo.FundVO;
@Service
public class FundDataServiceImpl implements FundDataService{
	@Autowired
	FundDataReporsitory fundDataReporsitory;
	@Autowired
	FundRepository fundRepository;
	@Override
	public List<Fund> querySmart(String beginDate, String endDate) {
//		Iterator<Fund> iterator = fundRepository.findAll().iterator();
		List<Object[]> list = fundDataReporsitory.findData(beginDate,endDate);
		List<Fund> fundList = new ArrayList<Fund>();
		for(Object[] vo:list){
			fundList.add(fundRepository.findOne((String)vo[0]));
		}
//		List<Fund> list = new ArrayList<Fund>();
	/*	while(iterator.hasNext()){
			Fund f = iterator.next();
			if(list.size()>10) break;
			try {
				FundData min = fundDataReporsitory.findTop1ByCodeAndFundDateBetweenOrderByUnitValueAsc(f.getId(), beginDate, endDate)
						.get(0);
				FundData max = fundDataReporsitory.findTop1ByCodeAndFundDateBetweenOrderByUnitValueDesc(f.getId(), beginDate, endDate)
						.get(0);
				FundData now = fundDataReporsitory.findTop1ByCodeAndFundDateBetweenOrderByFundDateDesc(f.getId(), beginDate, endDate)
						.get(0);

				if(new JDateTime(max.getFundDate()).compareDateTo(new JDateTime(min.getFundDate()))<0
						&&max.getUnitValue()>now.getUnitValue()){
					list.add(f);
				}
			} catch (Exception e) {
				continue;
			}
		}*/
		return fundList;
	}
	@Override
	public List<FundVO> querySmartOrderByRangeData(String beginDate,
			String endDate) {
		List<Object[]> list = fundDataReporsitory.findData(beginDate,endDate);
		List<FundVO> fundVOlist = new LinkedList<FundVO>();
		for(Object[] o:list){
			FundVO vo = new FundVO((String)o[0], 
					(Double)o[1], 
					(Double)o[2], 
					(Double)o[3],
					(Double)o[4],
					(String)o[5]);
			fundVOlist.add(vo);
		}
		return fundVOlist;
	}

}
